﻿/**********************************************************\
*  TinyLib - Entity                                        *
*                                                          *
*  Base class for graphical entities.                      *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AALib.Sprite;

namespace AALib.Entity
{
    public class Entity : DrawableGameComponent, IDrawable, IUpdateable
    {
        #region Fields

        protected Texture2D m_texture = null;
        protected SpriteBatch m_spritebatch;
        public Color m_color = Color.White;
        private AnimatedSprite m_animatedSprite = null;
        public Rectangle m_rectangle;

        private bool m_isVisible = true;
        private bool m_killMe = false;

        public Vector2 m_direction = Vector2.Zero;
        public Vector2 m_position = Vector2.Zero;
        public Vector2 m_origin = Vector2.Zero;

        private float m_scale = 1.0f;
        public float m_rotation = 0.0f;
        public float m_speed = 1.0f;
        
        public float m_density = 0.0f;
        public float m_fGravity = 0.1f;
        public float m_fGravityAcceleration = 1.0f;

        public Rectangle m_screenBoundaries = new Rectangle();

        #endregion

        #region Properties

        public bool KillMe
        {
            get { return m_killMe; }
            set { m_killMe = value; }
        }

        public Rectangle Rectangle
        {
            get { return m_rectangle; }
            set { m_rectangle = value; }
        }

        public Vector2 Direction
        {
            get { return m_direction; }
            set { m_direction = value; }
        }

        public AnimatedSprite AnimatedSprite
        {
            get { return m_animatedSprite; }
            set { m_animatedSprite = value; }
        }

        public Color Color
        {
            get { return m_color; }
            set { m_color = value; }
        }

        public Vector2 Origin
        {
            get { return m_origin; }
            set { m_origin = value; }
        }

        public float Scale
        {
            get { return m_scale; }
            set { m_scale = value; }
        }

        public SpriteBatch Spritebatch
        {
            get { return m_spritebatch; }
        }

        public Texture2D texture2D
        {
            get { return m_texture; }
            set { m_texture = value; }
        }

        public float Rotation
        {
            get { return m_rotation; }
            set { m_rotation = value; }
        }

        public bool IsVisible
        {
            get { return m_isVisible; }
            set { m_isVisible = value; }
        }

        public Vector2 Position
        {
            get { return m_position; }
            set { m_position = value; }
        }

        #endregion

        #region Init

        public Entity(Game p_game, SpriteBatch p_spritebatch)
            : base(p_game)
        {
            this.m_spritebatch = p_spritebatch;
        }

        #endregion

        #region Methods

        public override void Update(GameTime p_gameTime)
        {
            base.Update(p_gameTime);
            
            if (AnimatedSprite != null)
            {
                m_position = AnimatedSprite.m_position;
                Vector2 size = new Vector2(texture2D.Width / AnimatedSprite.FrameSize.X * AnimatedSprite.m_scale.X, texture2D.Height / AnimatedSprite.FrameSize.Y * AnimatedSprite.m_scale.Y);
                Rectangle = new Rectangle((int)m_position.X, (int)m_position.Y, (int)size.X, (int)size.Y);
            }
            else if (texture2D != null)
            {
                Rectangle = new Rectangle((int)m_position.X, (int)m_position.Y, (int)texture2D.Width, (int)texture2D.Height);
            }
            else
            {
                //nothing to do
            }
        }

        public override void Draw(GameTime p_gameTime)
        {
            if (AnimatedSprite != null)
            {
                m_animatedSprite.Draw(p_gameTime);
            }
            else if (texture2D != null)
            {
                m_spritebatch.Draw(m_texture, m_position, null, m_color, m_rotation, m_origin, m_scale, SpriteEffects.None, 0.0f);
            }
            else
            {
                //nothing to draw
            }
            
        }
        /*
        public void DrawAnim(GameTime gameTime)
        {
            animatedSprite.Draw(gameTime);
        }
        */
       // public abstract void OnCollision();

        #endregion
    }
}
